<?php
/**
 * Author: bedirhan
 * 
 * This class represents the automatic sql injector benchmarking (sqlibench) 
 * report
 * WARNING: PHP 5 only!
 * Reference: http://us2.php.net/manual/en/book.dom.php
 */

    class SqliBench{
        
        // DOM representation of the benchmarking xml file
        private $sqlibenchDOM;
        // XPath context of the benchmarking xml file
        private $sqlibenchXPath;
        
        function SqliBench($xmlFile){
            $this->sqlibenchDOM = new DOMDocument;
            $this->sqlibenchDOM->preserveWhiteSpace = false;
            $this->sqlibenchDOM->Load($xmlFile);
            $this->sqlibenchXPath = new DOMXPath($this->sqlibenchDOM);
            
        }
        
        // a general query method
        function query($query){
            return $this->sqlibenchXPath->query($query);
        }
        
        // No need to this method anymore, call getLabels(array()) for the same effect
        // return labels of the first level direct children of sqlibench root 
        // element. Those are category names.
        //function getCategories(){
        //    $categoryLabels = array();
        //    $categoryElements = $this->sqlibenchXPath->query("/sqlibench/*");
        //    foreach ($categoryElements as $element)
        //      array_push($categoryLabels, trim($element->attributes->getNamedItem('label')->textContent));
        //    return $categoryLabels;
        //}
        
        // return labels of the direct children of a evaluated target element. 
        // The target element is evaluated from a given "label path"
        // $labelPath is an array of labels, such as;
        // Performance, DBMS Banner, Blind, MSSQL
        // OR
        // Performance, DBMS Banner, Blind, MSSQL, sqlmap v0.5
        function getLabels($labelPath){
            $labels = array();
            
            $query = "/sqlibench/";
            // traverse each given label one by one
            foreach($labelPath as $label){
              // as you traverse, form a query 
              $query .= "*[@label='" .trim($label). "']/";
            }   
            $query .= "*" ;
            
            $categoryElements = $this->query($query);
            
            foreach ($categoryElements as $element)
              array_push($labels, trim($element->attributes->getNamedItem('label')->textContent));
            
            return $labels;
        }
        
        // the evaluated elements might be leaf elements so, we should 
        // return the text not the label. In order to understand whether a
        // target element is leaf or not, call getLabels method first, if it
        // returns empty array then the target element is a leaf and it's content
        // should be returned
        function getContent($labelPath){
            
            $query = "/sqlibench/";

            // traverse each given label one by one
            foreach($labelPath as $label){
              // as you traverse, form a query 
              $query .= "*[@label='" .trim($label). "']/";
            }   

            $query = substr($query, 0, strlen($query) - 1);
            
            $categoryElements = $this->query($query);
            $element = $categoryElements->item(0);
            
            return trim($element->textContent);            
        }
        
        // xsl transformation for a given xsl file on $sqlibenchDOM
        function transform($xslFile){
            $xp = new XsltProcessor();
            
            // create a DOM document and load the XSL stylesheet
            $xsl = new DomDocument;
            $xsl->load($xslFile);

            // import the XSL styelsheet into the XSLT process
            $xp->importStylesheet($xsl);
            
            // transform the XML into HTML using the XSL file
            // if the DOM exists
            if ($this->sqlibenchDOM && ($html = $xp->transformToXML($this->sqlibenchDOM)) ) 
              return $html;

        }
                
    }
    
?>
